package cn.northsea.service.impl;

import cn.northsea.common.Result;
import cn.northsea.entity.Seek;
import cn.northsea.entity.SeekDz;
import cn.northsea.entity.SeekUser;
import cn.northsea.mapper.SeekMapper;
import cn.northsea.mapper.UserMapper;
import cn.northsea.service.SeekService;
import cn.northsea.service.UserService;
import cn.northsea.utils.JwtUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static cn.northsea.common.enums.ResultCodeEnum.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SeekUser> implements UserService {
    @Autowired
    private UserMapper UserMapper;
    public Result psdLogin(SeekUser user, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<>();
        if (user == null) return  Result.error(PARAM_LOST_ERROR);
        SeekUser getuser = UserMapper.getUserByUserName(user);
        if(getuser==null) return Result.error(USER_NOT_EXIST_ERROR);
        if(getuser.getPassword().equals(user.getPassword())){
            String jwt = JwtUtils.createJwt(getuser);
            map.put("jwt","Bearer " + jwt);
            map.put("user",getuser);
            response.setHeader("Authorization", "Bearer " + jwt);
            response.setContentType("application/json; charset=utf-8");
        }else{
            return Result.error(USER_ACCOUNT_ERROR);
        }
        return Result.success(map);
    }

    @Override
    public void addInformation(SeekUser user) {
        UserMapper.addInformation(user);
    }

    @Override
    public SeekUser findJobByName(String username) {
       return UserMapper.findJobByName(username);
    }

    @Override
    public SeekUser findUserById(Long userId) {
        return UserMapper.selectById(userId);
    }

    @Override
    public SeekUser findUserByName(String username) {
        return UserMapper.findUserByName(username);
    }

    @Override
    public List<SeekUser> findAll() {
        return UserMapper.findAll();
    }

    @Override
    public void register(SeekUser user) {
        UserMapper.register(user);
    }

    @Override
    public List<SeekDz> userfirend(Integer uid) {
        return UserMapper.userfirend(uid);
    }

    @Override
    public SeekUser findByUserid(Integer toid) {
        return UserMapper.findByUserid(toid);
    }
}
